package hotel.data;

import hotel.exceptions.NoConnectionFoundException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class ConnectionByDriverManager
{
	private static String dbUserName = "root";
	private static String dbPassword = "q1w2e3";
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/";
	private static String dbName = "hoteldb?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=utf8&amp;connectionCollation=utf8_general_ci";
	
	private static final Logger logger = Logger.getLogger(ConnectionByDriverManager.class);
	
	private static final String CONNECTION_ERROR = "Error in ConnectionByDriverManager: "; 

	//private static Connection conn = null;

	public static Connection getConnection() throws NoConnectionFoundException
	{
		try 
		{
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url + dbName, dbUserName,
					dbPassword);
			return conn;
		}
		catch (SQLException | ClassNotFoundException e) 
		{
        	logger.error(ConnectionByDriverManager.CONNECTION_ERROR+ e.getMessage());
        	return null;
		}
	}

}
